<template>
  <el-dialog
    :title="title"
    :visible.sync="addVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @click="handleCancel"
  >
    <el-form :model="roleInfo">
      <el-form-item label="角色id" :label-width="formLabelWidth" v-if="!isAdd">
        <el-input v-model="roleInfo.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="角色名称" :label-width="formLabelWidth">
        <el-input v-model="roleInfo.roleName"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { roleInfoByIdApi, addRoleApi, editRoleApi } from "@/Api/RoleApi";
export default {
  props: ["addVisible", "title"],
  data() {
    return {
      roleInfo: {
        id: 0,
        roleName: "",
      },
      isAdd: true,
      formLabelWidth: "120px",
    };
  },
  methods: {
    handleCancel() {
      // this.resetForm();
      this.$emit("closeAddDialog");
    },
    async handleSave() {
      try {
        let res;
        if (this.isAdd) {
          res = await addRoleApi(this.roleInfo);
        } else {
          res = await editRoleApi(this.roleInfo);
        }

        if (res.status == 200) {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 200,
            onClose: () => {
              this.$emit("closeAddDialog", true);
            },
          });
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 1000,
          });
        }
      } catch (error) {
        this.loading = false;
        this.$message({
          message: "系统异常",
          type: "error",
          duration: 1000,
        });
      }
    },
    async openDialog(roleId) {
      if (!roleId) {
        this.roleInfo = {
          id: 0,
          roleName: "",
        };
        this.isAdd = true;
      } else {
        this.isAdd = false;
        try {
          this.loading = true;
          const res = await roleInfoByIdApi(roleId);
          if (res.status == 200) {
            this.roleInfo = res.data;
            this.loading = false;
          } else {
            this.loading = false;
            this.$message({
              message: res.msg,
              type: "error",
              duration: 1000,
            });
          }
        } catch (error) {
          this.loading = false;
          this.$message({
            message: "系统异常",
            type: "error",
            duration: 1000,
          });
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>    